<?php
class Credit_Cards_model extends MY_Model
{
	public
		$table = 'credit_cards',
		$prKey = 'id',
		
		$credit_cards = array(			
			'1' => 'MasterCard',
			'2' => 'VISA Electron',
			'3' => 'JCB',
			'4' => 'VISA/Dankort',
		)
	;

	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'id'		 		=> '',
			'id_user'		 	=> T_('User ID'),
			'id_role'		 	=> T_('User Role'),
			'first_name' 		=> T_('First Name'),
			'last_name' 		=> T_('Last Name'),
			'card_type'			=> T_('Card Type'),
			'card_number'		=> T_('Card Number'),
			'card_id_number' 	=> T_('Card ID Number'),
			'date_expired'		=> T_('Date expired'),
			'year_expired'		=> T_('Year'),
			'month_expired'		=> T_('Month'),
			'is_default_card' 	=> T_('Default card?'),
		);

		$this->rules = array(
			'id_user' 			=> "trim|required",
			'id_role' 			=> "trim|required",
			'first_name' 		=> "trim|required|max_length[255]",	
			'last_name' 		=> "trim|required|max_length[255]",
			'card_type' 		=> "trim|required|is_natural_no_zero",
			'card_number' 		=> "trim|required|max_length[32]",
			'card_id_number' 	=> "trim|required|max_length[32]",
			'year_expired'		=> "trim|required|is_natural_no_zero|max_length[4]",
			'month_expired'		=> "trim|required|is_natural_no_zero|max_length[2]",
			'is_default_card' 	=> "trim",
		);
	}

	function save($data)
	{
		if (isset($data['password']))
		{
			$data['password'] = $this->__cryptPassword($data['password']);
		}

		parent::save($data);
	}
	
	function search($config = array())
	{
		$id_user = $config['id_user'];
		$date_start = $this->input->get_post('date_start');
		$date_end = $this->input->get_post('date_end');
		
		$order 		= $this->input->get_post('order');
		$direction 	= $this->input->get_post('direction');

		// main sql		
		$sql = '';
		$sql .= ' FROM '.$this->table.' cc';
		//$sql .= ' LEFT JOIN  1 = 1';
		$sql .= ' WHERE 1 = 1';  

		if (!empty($config['id_user']))
		{
			$sql .= ' AND cc.id_user='.$this->db->escape($id_user); 
		}
		
		if ($date_start && $date_end)
		{
			$sql .= ' AND cc.date_created>='.$this->db->escape($date_start). ' AND cc.date_created<='.$this->db->escape($date_end); 
		}
		else if ($date_start)
		{
			$sql .= ' AND cc.date_created>='.$this->db->escape($date_start); 
		}
		else if ($date_end)
		{
			$sql .= ' AND cc.date_created<='.$this->db->escape($date_end); 
		}
		
		switch ($order)
		{
			case 'date':
				$sql .= ' ORDER BY cc.date_created'; 
				break;
			
			default:
				$sql .= ' ORDER BY cc.id';
				$direction = 'DESC';
		}
		
		$sql .= ($direction == 'DESC') ? ' DESC' : ' ASC';
		
		// main sql
		$sql = "SELECT * " . $sql;// . sprintf(' LIMIT %d, %d', $per_page, $config['per_page']); 

		$this->data['list'] = $this->db->query($sql)->result();
	}		
	
  function delete($items_serialized = null)
  {
    $items = explode("&", $items_serialized);
    
    $report = "";
    foreach ($items as $id)
    { 
      $res = $this->db->delete($this->table, array($this->prKey => $id));
      
      if (is_null($res))
      {
        $this->log->writeMessage('error', T_('Error. Credit Card was not removed.'));
        $report .= ",0"; // don't deleted
      }
      else
      {
        $this->log->writeMessage('done', T_('Credit Card was removed.'));
        $report .= ",1"; // deleted       
      } 
    }
    
    return substr($report, 1);
  }
  	
  function insert($data)
  {
    if (!empty($data))
    {
      $data['date_created'] = date('Y-m-d H:i:s');    
      
      parent::insert($data);

      if ($this->db->insert_id() > 0)
      {
        $this->log->writeMessage('done', T_('Credit Card was added succesfully.'));
		
        return TRUE;
      }
      else
      {
        $this->log->writeMessage('error', T_('Error! Credit Card was not created.'));
        
        return FALSE;
      }
    }   
  }

  function update($data)
  {
    if (!empty($data))
    {   
      $res = parent::update($data);

      $this->log->writeMessage('done', T_('Saved succesfully.'));
		
      return TRUE;      
    }   
  } 
}
?>